###### Estimating Trust in Government, 1958-2022
###### Running Stan Models Using cmdstanr
###### v5

# libraries
library(arm)
library(dplyr)
library(tidyr)
library(loo)
library(ggplot2)
library(bayesplot)
library(cmdstanr)
library(rio)
library(tidyverse)

setwd("C:/Users/ba72loko/projects/Democratic Resilience (with Aurel)/data/political trust BJPS study")

# options
options("cmdstanr_verbose" = TRUE)
options(mc.cores = parallel::detectCores())

# read trust data
trustgov = read.csv("trends_gov.csv")

## Edit data

# remove NAs
trustgov = trustgov[!trustgov$Response==0, ]

# order
trustgov = arrange(trustgov, Country, Year)

# I originally used the item variable with wording variation as the item variable for this analysis
# Use a broader item variable instead, which only uses the survey source (as Chris Claassen does), generally results in more dynamic (less flat) trends
trustgov <- dplyr::select(trustgov, -Item)
trustgov <- dplyr::rename(trustgov, Item = Project)

# Recode the value of Item to make the EB and AsiaBarometer data source identifiers more specific
# Initially, the grep code for item-country length identifiers below thought that many other observations were also from these sources
trustgov$Item[trustgov$Item=="EB"] <- "SSEB" # stands for "Standard and Special Eurobarometer
trustgov$Item[trustgov$Item=="AsB"] <- "AsiaB" # stands for "Standard and Special Eurobarometer

# set first year
year0gov = 1957 # = year before first year of available survey data
trustgov = trustgov[trustgov$Year > year0gov,]

# create item by country indicators
trustgov = unite(trustgov, ItemCnt, c(Item, Country), sep = "_", remove = FALSE)

# create item by region indicators
trustgov = unite(trustgov, ItemReg, c(Item, regpol6), sep = "_", remove = FALSE)

# identify countries with few years of data
cnt.obs.years = rowSums(table(trustgov$Country, trustgov$Year) > 0)
sort(cnt.obs.years)

# run the next line to drop countries with less than 2 years of data
trustgov = trustgov[trustgov$Country %in% levels(factor(trustgov$Country))[cnt.obs.years > 1], ]
length(unique(trustgov$Country))

## Prepare data for stan

# factorise
trustgov$Country = as.factor(as.character(trustgov$Country))
trustgov$Item = as.factor(as.character(trustgov$Item))
trustgov$ItemCnt = as.factor(as.character(trustgov$ItemCnt))
trustgov$Year = trustgov$Year-year0gov

# extract data
n.items = length(unique(trustgov$Item))
n.cntrys = length(unique(trustgov$Country))
n.yrs = 2020-year0gov # estimates up to 2020
n.resp = dim(trustgov)[1]
n.itm.cnt = length(unique(trustgov$ItemCnt))
cntrys = as.numeric(factor(trustgov$Country))
cnt.names = levels(trustgov$Country)
items = as.numeric(factor(trustgov$Item))
yrs = trustgov$Year
itm.cnts = as.numeric(factor(trustgov$ItemCnt))
mean.resp.log = logit(mean(trustgov$Response))

# create item-country length indicator for items
item.ind.kp = rep(0, length(levels(trustgov$ItemCnt)))
for(i in 1:length(levels(trustgov$Item))) {
  item.ind.kp[grepl(levels(trustgov$Item)[i], levels(trustgov$ItemCnt))] = i
}
item.ind.len = sapply(lapply(levels(trustgov$Item), function(x) grep(x, levels(trustgov$ItemCnt))), length)

## Fit stan model

# specify data for stan
dat.1 = list(N=n.resp, K=n.items, R=n.yrs, J=n.cntrys, P=n.itm.cnt, jj=cntrys, rr=yrs, 
             pp=itm.cnts, kk=items, it_len=item.ind.len, 
             x=trustgov$RespN, samp=trustgov$Sample, mn_resp_log=mean.resp.log)
sapply(dat.1, summary)

# parameters to save 
pars.1 = c("Sigma","Omega","sigma_delta","sigma_theta","phi","mu_lambda","lambda","gamm","delta",
           "theta","x_pred","log_lik")

# iterations for MCMC simulation
n.iter = 1000
n.warm = 500
n.samp = n.iter - n.warm
n.chn = 4

# compile model
stan.mod = cmdstan_model('stan.mod6.7_newstanrcode.stan')

# Stan fit
fit.mod= stan.mod$sample(
  data = dat.1,
  chains = n.chn,
  init = 0.1,
  parallel_chains = n.chn,
  iter_warmup = n.warm,
  iter_sampling = n.samp,
  refresh = round(n.iter/20, 0),
  adapt_delta = 0.99, 
  max_treedepth = 13,
  save_warmup = FALSE
)

## Check convergence

# Examine model fit
res = fit.mod
res$cmdstan_diagnose()
res.tab = res$print(pars.1, max_rows=80, digits=3)
sum = res$summary(pars.1)
print(sum[order(sum$rhat, decreasing=TRUE), ], n=50)
res_neff_ratio = neff_ratio(res)
res_neff_ratio[order(res_neff_ratio, decreasing=FALSE)][1:50]

# traceplot
tp.pars = c("Sigma[1,1]","Sigma[2,2]","Omega[1,2]","sigma_theta","sigma_delta","mu_lambda",
            "phi","delta[23]","theta[31,35]")
tp = bayesplot::mcmc_trace(res$draws(tp.pars), size=0.3, np=nuts_params(res))
tp

## Extract and save mood estimates
theta.m.out = apply(res$draws("theta"), 3, as.vector)
(theta.m.mean = mean(as.vector(theta.m.out)))
(theta.m.sd = sd(as.vector(theta.m.out)))
theta.m.std = (theta.m.out - theta.m.mean) / theta.m.sd # standardize
theta.m.t = apply(theta.m.std, 1, function(x) t(x) )
theta.pe = apply(theta.m.t, 1, mean)
theta.u95 = apply(theta.m.t, 1, quantile, probs=c(0.975))
theta.l95 = apply(theta.m.t, 1, quantile, probs=c(0.025))
theta.sd = apply(theta.m.t, 1, sd)
theta.m.df = data.frame(Country=rep(cnt.names, each=n.yrs), 
                        Year=rep(1958:2020, times=n.cntrys), gov=theta.pe, 
                        gov_u95=theta.u95, gov_l95=theta.l95, gov_sd=theta.sd)

# remove estimates before first survey year and create a trimmed dataset
first.yr = data.frame(Country = levels(trustgov$Country),
                      First_yr = as.vector(
                        by(trustgov, trustgov$Country, function(x) min(as.numeric(x$Year)) + year0gov)))
theta.trim = merge(theta.m.df, first.yr, by="Country", all.x=TRUE)
cnts = theta.trim[theta.trim$Year==2008, "Country"]
frst.yr = theta.trim[theta.trim$Year==2008, "First_yr"]
theta.trim$gov_trim = theta.trim$gov
theta.trim$gov_trim = ifelse(theta.trim$Year < theta.trim$First_yr, NA, theta.trim$gov_trim)
theta.trim = theta.trim[order(theta.trim$Country, theta.trim$Year), ]
theta.trim = theta.trim[!is.na(theta.trim$gov_trim),]
theta.trim$gov_trim = NULL

# save country-year point estimates
write.csv(theta.trim, "gov_mood_est.csv", row.names=FALSE)

